define(['app'],function(app){
    return {module: function($scope, $http, eventBusService, httpService, params, configService) {
	    $scope.form = {};

		httpService.post(app.urls.menu_select,{"id":params.father_id}, function(data) {
			if("0000" === data.code){
				$scope.menu_name = data.data[0].name;
				$scope.findButton(data.data[0].id);
			}else{
				alert(data.msg);
			}
		});
		
		$scope.findButton = function(menu_id) {
			httpService.post(app.urls.button_select,{"menu_id":menu_id},function(data){
				if("0000" === data.code){
					$scope.buttons = data.data;
				}else{
					alert(data.msg);
				}
			});
		}
		
        $scope.find = function() {
            httpService.postpage(app.urls.stage_selectAll, $scope.form, function (data) {
                if ("0000" === data.code) {
                    $scope.dataList = data.data;
                } else {
                    alert(data.msg);
                }
            });
        }
        $scope.find();
        
        $scope.clickBtn = function(event){
    		if(event == 'add') {
    			$scope.add();
    		}else if(event == 'update') {
    			$scope.edit();
    		}else if(event == 'delete') {
    			$scope.remove();
    		}
    	}

        //新增
        $scope.add = function() {
            eventBusService.publish("modal.page", "modules/content/baseinfo/stage/add");
        }

        //编辑
        $scope.edit = function() {
            var values = [];
            $('input[name="dataPk"]:checked').each(function() {
                values.push($(this).val());
            });

            if(values.length < 1) {
                eventBusService.publish("modal.alert", {"content":"请选择一条数据。"});
            }else if(values.length > 1) {
                eventBusService.publish("modal.alert", {"content":"不能同时选择多行数据，请选择一条数据。"});
            }else{
                eventBusService.publish("modal.page", "modules/content/baseinfo/stage/edit?id="+values[0]);
            }
        }

        //删除
        $scope.remove = function() {
            var values = [];
            $('input[name="dataPk"]:checked').each(function(){
                values.push($(this).val());
            });
            if(values.length < 1) {
                eventBusService.publish("modal.alert", {"content":"请选择一条数据。"});
            }else if(values.length > 1){
                eventBusService.publish("modal.alert", {"content":"不能同时选择多行数据，请选择一条数据。"});
            }else{
                eventBusService.publish("modal.alert", {"content":"确认删除这条记录吗？", "isAutoClose":false,
                    "confirm":function(){
                        httpService.post(app.urls.stage_delete,{"id":values[0]}, function(data) {
                            if(data.code=="0000"){
                                eventBusService.publish("content.page.reload", data.data);
                            }else{
                                eventBusService.publish("modal.alert", {"content":"删除失败。"});
                            }
                        });
                    }
                });
            }
        }

        $scope.select = function() {
            $scope.page.current = 1;
            $scope.find();
        }

        //接收数据刷新
        eventBusService.subscribe('content.page.reload', function(event,data) {
            $scope.select();
        });
            //选择数据
            $scope.optData = function (element) {
                if($(element).find("input[name='dataPk']").is(":checked")){
                    $(element).find("input[name='dataPk']").prop("checked",false);
                    $(element).removeClass("opt_tr");
                }else{
                    $(element).find("input[name='dataPk']").prop("checked",true);
                    $(element).addClass("opt_tr");
                }
            }
            $scope.checkAll = function(target) {
                $("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
                    if ($(this).prop("checked")) {
                        $(this).prop('checked', false);
                        $(this).parents("tr").removeClass("opt_tr");
                    } else {
                        $(this).prop('checked', true);
                        $(this).parents("tr").addClass("opt_tr");
                    }
                })
            }
    }
}});